package com.cm.projectx.controller.pricetask;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cm.projectx.response.Result;
import com.cm.projectx.service.ITaskTemplateService;
import com.cm.projectx.vo.QueryTaskTemplateVO;
import com.cm.projectx.vo.TaskTemplateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/v2api/price/taskTemplate")
@Slf4j
public class TaskTemplateController {

    @Autowired
    private ITaskTemplateService taskTemplateService;


    /**
     * 三期接口保存模板
     */
    @RequestMapping("save")
    public Result saveTaskTemplate(@RequestBody TaskTemplateVo templateVo) {
        Result result = new Result();
        try {
            if (StringUtils.isBlank(templateVo.getTemplateName())) {
                result.setCode(-1);
                result.setMsg("模板名称不能为空");
                return result;
            }
          taskTemplateService.saveTemplate(templateVo.getTaskVo(), templateVo.getTemplateName());
            result.setCode(0);
            result.setMsg("保存模板成功");
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("保存模板失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 三期删除任务模板接口
     * @return
     */
    @RequestMapping("delete")
    public Result delTemplate(@RequestParam String templateCode){
        Result result = new Result();
        try {
            taskTemplateService.deleteTemplate(templateCode);
            result.setCode(0);
            result.setMsg("删除模板成功");
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("删除模板失败: " + e.getMessage());
            log.error("删除模板异常", e);
        }
        return result;
    }

    /**
     * 三期编辑任务模板接口(编辑并下发)
     */
    @RequestMapping("editAndIssue")
    public Result eidTemplate(@RequestBody TaskTemplateVo templateVo){
        Result result = new Result();
        try {
            return taskTemplateService.updateTemplate(templateVo);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("更新模板失败: " + e.getMessage());
            log.error("更新模板异常", e);
        }
        return result;
    }

    /**
     * 三期任务模板列表
     */
    @RequestMapping("list")
    public Result templateList(QueryTaskTemplateVO queryTaskTemplateVO){
        Result result = new Result();
        result.setCode(0);
        result.setMsg("业务成功");
        result.addData("list",  taskTemplateService.getTemplateList(queryTaskTemplateVO));
        return result;
    }

    /**
     * 查询详情
     */
    @GetMapping("getTemplateDetail")
    public Result getTemplateDetail(@RequestParam String templateCode) {
        return taskTemplateService.getTemplateDetail(templateCode);
    }
}
